import numpy as np
import cv2 as cv
img = cv.imread('../../data/multObj.png')

imgray =cv.cvtColor(img,cv.COLOR_BGR2GRAY)
ret,thresh=cv.threshold(imgray,127,255,0)
retrievalMode =cv.RETR_TREE
approxMethod=cv.CHAIN_APPROX_NONE
contours,hierarchy=cv.findContours(thresh,retrievalMode,approxMethod)


SymbolArea=11560
offset = round(SymbolArea*0.2)
validIdx=[]
for idx,cnt in enumerate(contours):
   area =cv.contourArea(cnt)
   print(idx,":",cnt.size,',',area)

   minP =SymbolArea - offset
   maxP =SymbolArea + offset
   if area >minP and area< maxP:
       validIdx.append(idx)

imgBackGround =np.zeros(img.shape,np.uint8)

for idx in validIdx:
   cv.drawContours(imgBackGround,contours,idx,(0,0,255),1)
cv.imshow("img", img)
cv.imshow("imgBG",imgBackGround)
cv.waitKey(0)
cv.destroyAllWindows()